package com.study.demo;



import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Stream;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.stage.Stage;

/**
 * 查找关键字和排序例子
 * @author Administrator
 *
 */
public class FindKeyAndSortDemo extends Application{
	String oldKey="";
	int index=0;

	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {
		HBox hBox = new HBox();

		TextField keyField = new TextField();
		Button keyButton = new Button("查找");
		Button sortButton = new Button("排序");
		HBox.setMargin(keyButton, new Insets(10));

		hBox.getChildren().addAll(keyField,keyButton,sortButton);
		hBox.setAlignment(Pos.CENTER);

		VBox vBox = new VBox();
		TextArea text = new TextArea();
		text.setPrefHeight(200);
		text.setPrefWidth(350);
		vBox.getChildren().addAll(hBox,text);
		Label tipLabel = new Label();
		//设置Label的字体颜色
		tipLabel.setTextFill(Paint.valueOf("#CD6600"));
		vBox.getChildren().add(tipLabel);

		AnchorPane aPane = new AnchorPane();
		aPane.getChildren().add(vBox);
		AnchorPane.setLeftAnchor(vBox, 20.0);
		AnchorPane.setLeftAnchor(vBox, 20.0);


		stage.setScene(new Scene(aPane,400,300));
		stage.show();


		keyField.setText("af");
		text.setText("abafcdefafdeftafdefgaf");



		//查找
		keyButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				String keyStr = keyField.getText();
				String textStr = text.getText();
				//如果第二次查询的关键字和上次查询的关键字不一致，则从新查找
				if(!keyStr.equals(oldKey)){
					index=0;
					oldKey = keyStr;
				}

				int i = textStr.indexOf(keyStr, index);
				if(i > -1){
					tipLabel.setText("查找到了");
					text.requestFocus();
					text.selectRange(i,keyStr.length() + i) ;
					index = keyStr.length() + i;
				}else{
					tipLabel.setText("没有找到对应的关键字");
					index=0;
				}
			}
		});


		//排序
		sortButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {


				char[] chars = text.getText().toCharArray();
				//
				List<String> list = new ArrayList<String>();
				for(char c:chars){
					list.add(String.valueOf(c));
				}

				//1.0笨拙的处理方式
			/*	list.sort(new Comparator<String>() {
					@Override
					public int compare(String o1, String o2) {
						return o1.compareTo(o2);
					}
				});
				text.setText("");
				for (String s:list) {
					text.appendText(s);
				}*/



				//2.0 更加灵活的处理方式(lamda表达式)
				text.setText("");
				//list.stream().sorted((o1,o2) -> o1.compareTo(o2)).forEach(o1 -> text.appendText(o1));

				//分开写也是可以的
				Stream<String> stream= list.stream().sorted(new Comparator<String>() {

					@Override
					public int compare(String o1, String o2) {
						return o1.compareTo(o2);
					}
				});
				stream.forEach(o1 -> text.appendText(o1));
			}
		});

	}
}
